const Router = require("koa-router");
const router = new Router();
const callCloudDB = require("../utils/callCloudDB");
const callCloudStorage = require("../utils/callCloudStorage");

router.get("/list", async (ctx, next) => {
  const params = ctx.request.query;
  const query = `db.collection('blog').skip('${params.start}').limit('${params.count}').orderBy('createTime', 'desc').get()`;

  const res = await callCloudDB(ctx, "databasequery", query);

  ctx.body = {
    data: res.data,
    code: 20000
  };

});

router.post("/del", async (ctx, next) => {
  const params = ctx.request.body;
  // 删除blog
  const queryBlog = `db.collection('blog').doc('${params._id}').remove()`;
  const delBlogReg = await callCloudDB(ctx, "databasedelete", queryBlog);

  // 删除blog-comment
  const queryBlogComment = `db.collection('blog-comment').where({
    blogId:'${params._id}'
  }).remove()`;
  const delBlogCommentReg = await callCloudDB(ctx, "databasedelete", queryBlogComment);

  // 删除图片
  const delStorageRes = await callCloudStorage.delete(ctx, params.img);

  ctx.body = {
    code: 20000,
    data: {
      delBlogReg,
      delBlogCommentReg,
      delStorageRes
    }
  }

});

module.exports = router;